//
//  MealViewController
//  TestSwift3
//
//  Created by ficow on 14/8/16.
//  Copyright © 2016年 ficow. All rights reserved.
//

import UIKit

class MealViewController: UIViewController,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate {
    
    // MARK: - PROPERTIES
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var photoIV: UIImageView!
    @IBOutlet weak var ratingControl: RatingControl!
    @IBOutlet weak var saveBtn: UIBarButtonItem!
    
    var meal:Meal?
    
    // MARK: - VC LIFE CYCLE
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        
        textField.delegate = self;
        if let meal = meal {
            navigationItem.title = meal.name
            textField.text = meal.name
            photoIV.image = meal.photo
            ratingControl.rating = meal.rating
            
        }
        
        checkValidMealName()
    }
    
    // MARK: - ACTIONS
    @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
        textField.resignFirstResponder()
        let imagePickerCtrl = UIImagePickerController()
        imagePickerCtrl.sourceType = .photoLibrary
        imagePickerCtrl.delegate = self
        present(imagePickerCtrl, animated: true, completion: nil)
    }
    
    @IBAction func cancelBtnPressed(_ sender: UIBarButtonItem) {
        let isPresentingInAddMealMode = presentingViewController is UINavigationController
        if isPresentingInAddMealMode{
            dismiss(animated: true, completion: nil)
        }else{
            navigationController!.popViewController(animated: true)
        }
        
    }
    
    // MARK: - IMG PICKER CTRL DELEGATE
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let selectedIMG = info[UIImagePickerControllerOriginalImage] as! UIImage
        photoIV.image = selectedIMG
        dismiss(animated: true, completion: nil)
    }
    
    // MARK: - TEXTFIELD DELEGATE
    func checkValidMealName(){
        let text = textField.text ?? ""
        saveBtn.isEnabled = !text.isEmpty
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true;
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        checkValidMealName()
        navigationItem.title = textField.text
    }

    // MARK: NAVIGATION
    override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        if saveBtn === sender{
            let name = textField.text ?? ""
            let photo = photoIV.image
            let rating = ratingControl.rating
            
            meal = Meal(name: name, photo: photo, rating: rating)
        }
    }
    
    
    // MARK: NAVI POP GESTURE DELEGATE
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        let isPresentingInAddMealMode = presentingViewController is UINavigationController
        if isPresentingInAddMealMode{
            dismiss(animated: true, completion: nil)
        }else{
            navigationController!.popViewController(animated: true)
        }
        return true
    }
}

